import requests
import json
from io import BytesIO
import pandas as pd
gaode_key = "d51fbd0868d98deca1f02899f79c6e8e"


## 地理编码api调用函数
def geocode(key,address,city=None,batch=None,sig=None,output="JSON",callback=None):
    """获取高德API的地理编码
    注释：key是指高德API的秘钥，请先去注册
          address是指结构化地址，具体请查看：https://lbs.amap.com/api/webservice/guide/api/georegeo   
    """
    url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
    "key":key,
    "address":address,
    "city":city,
    "batch":batch,
    "sig":sig,
    "output":output,
    "callback":callback
    }
    
    response = requests.get(url,params=params)
    results = response.json()[ 'geocodes'][0][ 'location']
    return results

## 周边查询api调用函数
def around(key,location,keywords=None,types=None,city=None,radius=3000,sortrule="distance",offset=20,page=1,extensions="base",sig=None,output="JSON",callback=None):
    around_url = "https://restapi.amap.com/v3/place/around?parameters"
    params = {
        "key":key,
        "location":location,
        "keywords":keywords,
        "types":types,
        "city":city,
        "radius":radius,
        "sortrule":sortrule,
        "offset":offset,
        "page":page,
        "extensions":extensions,
        "sig":sig,
        "output":output,
        "callback":callback
    }
    
    r = requests.get(around_url,params=params)
    result = r.json()
    return result

## 天气查询api调用函数
def weather(city,key="d51fbd0868d98deca1f02899f79c6e8e",extensions=None,output="JSON"):
    weather_url = " https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
        "key":key,
        "city":city,
        "extensions":extensions,
        "output":output
    }
    
    r = requests.get(weather_url,params=params)
    result = r.json()
    return result

# 步行路径规划api调用函数
def walking(key,origin,destination,sig=None,output="JSON",callback=None):
    """获取高德API的步行路径规划
    注释：key是指高德API的秘钥，请先去注册
          origin和destination是指出发点和目的地，具体请查看：https://lbs.amap.com/api/webservice/guide/api/direction   
    """
    url = "https://restapi.amap.com/v3/direction/walking?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,
        "sig":sig,
        "output":output,
        "callback":callback
    }
    
    response = requests.get(url,params=params)
    results = response.json()["route"]["paths"][0]["steps"]
    a = []
    for item in results:
        a.append(item["instruction"])
    return a


# 驾车路径规划api调用函数
def driving(key,origin,destination,originid=None,destinationid=None,origintype=None,destinationtype=None,strategy=10,waypoints=None,avoidpolygons=None,avoidroad=None,province=None,number=None,cartype=0,ferry=None,roadaggregation=None,extensions=None,nosteps=0,sig=None,output="JSON"):
    truck_url = "https://restapi.amap.com/v3/direction/driving?parameters"
    params ={
        "key":key,
        "origin":origin,
        "destination":destination,
        "extensions":extensions,
        "originid":originid,
        "destinationid":destinationid,
        "origintype":origintype,
        "destinationtype":destinationtype,
        "strategy":strategy,
        "waypoints":waypoints,
        "avoidpolygons":avoidpolygons,
        "avoidroad":avoidroad,
        "province":province,
        "number":number,
        "cartype":cartype,
        "nosteps":nosteps,
        "ferry":ferry,
        "output":output,
        "sig":sig
    }     
    r = requests.get(truck_url,params=params)
    result = r.json()['route'][ 'paths'][0]['steps']
    a=[]
    for i in result:
        a.append(i['instruction'])
    return a